package routers

import (
	"gin/common"
	"gin/controller"
	"gin/middleware"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"io"
	"os"
	"time"
)

// SetupRouter 配置路由信息
func SetupRouter() *gin.Engine {
	//设置模式
	gin.SetMode(gin.DebugMode)
	r := gin.Default()
	r.Handle("GET", "/hello", func(context *gin.Context) {

	})
	//处理跨域
	r.Use(common.Cors())
	//框架内的跨域处理
	r.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"*"},
		AllowMethods:     []string{"PUT", "PATCH"},
		AllowHeaders:     []string{"Origin"},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
		AllowOriginFunc: func(origin string) bool {
			return origin == "https://github.com"
		},
		MaxAge: 12 * time.Hour,
	}))

	//处理捕捉500报错
	r.Use(common.Recover)
	//控制台日志都会生成在这个文件
	file, _ := os.Create("log.1.log")
	gin.DefaultWriter = io.MultiWriter(file)
	r.POST("/upload", controller.Upload)
	r.POST("/reg", controller.Register)
	r.GET("/test", controller.Test)
	r.GET("/wxLogin", controller.WxLogin) //允许访问所有域
	r.POST("/login", controller.TestLogin)
	apiGroup := r.Group("/", middleware.SignMiddleware())
	{
		//微信相关
		wxGroup := apiGroup.Group("/wx")
		{
			wxGroup.GET("/pack", controller.GetSignPackage)
			wxGroup.GET("/service", controller.WxService)
		}
		//店铺相关
		userGroup := apiGroup.Group("/shop")
		{
			userGroup.GET("/list", controller.ShopList)
			userGroup.POST("/list", controller.ShopList)
		}

		//商品相关
		goodsGroup := apiGroup.Group("/goods")
		{
			goodsGroup.GET("/list", controller.GoodsList)
			goodsGroup.GET("/detail", controller.GoodsDetail)
			goodsGroup.GET("/price", controller.CalculatePrice)
		}
		//启用中间件
		//会员相关
		memberGroup := apiGroup.Group("/member", middleware.AuthMiddleware())
		{
			memberGroup.POST("/info", controller.Info)
			memberGroup.POST("/score", controller.GetScoreList)
			memberGroup.POST("/recommend", controller.GetRecommendList)
			memberGroup.POST("/signList", controller.GetSignList)
			memberGroup.POST("/signIn", controller.SignIn)

		}

		//购物车相关
		cartGroup := apiGroup.Group("/cart", middleware.AuthMiddleware())
		{
			cartGroup.POST("/add", controller.AddCard)
			cartGroup.POST("/list", controller.CardList)
			cartGroup.POST("/modify", controller.ModifyNumber)
			cartGroup.POST("/empty_cart", controller.EmptyCart)
			cartGroup.POST("/remove", controller.Remove)

		}
		//订单
		orderGroup := apiGroup.Group("/order", middleware.AuthMiddleware())
		{
			orderGroup.POST("/calculate", controller.Calculate)
			orderGroup.POST("/add", controller.OrderAdd)
			orderGroup.POST("/list", controller.OrderList)
			orderGroup.POST("/cancel", controller.CancelOrder)
			orderGroup.POST("/confirm", controller.ConfirmOrder)
			orderGroup.POST("/remove", controller.RemoveOrder)
		}

		//收货地址相关
		addressGroup := apiGroup.Group("/address", middleware.AuthMiddleware())
		{
			addressGroup.POST("/add", controller.AddAddress)
			addressGroup.POST("/list", controller.AddressList)
			addressGroup.POST("/modify", controller.ModifyAddress)
			addressGroup.POST("/empty_cart", controller.EmptyCart)
			addressGroup.POST("/remove", controller.RemoveAddress)
		}
		//优惠券
		couponsGroup := apiGroup.Group("/coupons", middleware.AuthMiddleware())
		{
			couponsGroup.POST("/list", controller.GetCouponsList)
			couponsGroup.POST("/my", controller.GetMyCouponsList)
			couponsGroup.POST("/receive", controller.ReceiveCoupons)
		}

		//公共
		commonGroup := apiGroup.Group("/common")
		{
			commonGroup.GET("/province", controller.GetProvince)
			commonGroup.GET("/child", controller.GetChild)
			commonGroup.GET("/page", controller.GetPage)
			commonGroup.GET("/notice", controller.GetNotice)
			commonGroup.GET("/banner", controller.GetBanner)
			commonGroup.GET("/location", controller.GetLocation)
		}

		payGroup := apiGroup.Group("/pay", middleware.AuthMiddleware())
		{
			payGroup.POST("/wx", controller.WxJsapi)
		}
	}
	return r
}
